package com.zzy.backend.controller.user;

import com.zzy.backend.mapper.UserMapper;
import com.zzy.backend.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author molimark<br />
 * @date: 2022/12/6 11:50<br/>
 * @description: <br/>
 */
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/user/all/")
    public List<User> getAll(){
        return userMapper.listUsers();
    }

    @GetMapping("/user/{userId}/")
    public User getUser(@PathVariable int userId){
        return userMapper.getUser(userId);
    }

    @GetMapping("/user/add/{userId}/{username}/{password}/")
    public void addUser(@PathVariable int userId,@PathVariable String username,@PathVariable String password){
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encodedPassword = passwordEncoder.encode(password);
        User user = new User(userId,username,encodedPassword,null);
        userMapper.addUser(user);
    }
}
